package com.cats.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.cats.R;

public class NewAddressActivity extends BaseActivity implements android.view.View.OnClickListener, 
AdapterView.OnItemSelectedListener {
	private String array_spinner[];

	public NewAddressActivity() {
		// TODO Auto-generated constructor stub
	}

	@SuppressWarnings("unchecked")
	protected void onCreate(Bundle savedInstanceState) {
		System.out.println("[NewAddressActivity]onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_address);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		setActionBarTitle("New Address");
		
		Button saveButton = (Button)findViewById(R.id.btnSave);
		saveButton.setOnClickListener((android.view.View.OnClickListener)this);
		// Test
		array_spinner = new String[3];
		array_spinner[0]="United States";
		array_spinner[1]="Canada";
		array_spinner[2]="Other";
		
		Spinner s = (Spinner) findViewById(R.id.countrySpinner);
		ArrayAdapter adapter = new ArrayAdapter(
				this,
				android.R.layout.simple_spinner_dropdown_item, 
				array_spinner);
		s.setOnItemSelectedListener(this);
		adapter.setDropDownViewResource(
		   android.R.layout.simple_spinner_dropdown_item);
		s.setAdapter(adapter);	
	}

	public void onItemSelected(AdapterView<?> parent, View v, int position,
			long id) {
		System.out.println("[NewAddressActivity]Select Country:" + parent.getItemAtPosition(position).toString());
	}

	public void onNothingSelected(AdapterView<?> parent) {
	}
	
	private boolean varifyInputContent() {
		EditText nameEditText = (EditText)findViewById(R.id.txtName);
		String nameString = nameEditText.getText().toString();
		if (nameString.replaceAll("\\s+", "").length() == 0) {
			showWarningWithMsg("Please inpt your full name.");
			return false;
		} // 1. check name
		
		EditText addressEditText = (EditText)findViewById(R.id.txtAddressLine1);
		String addressString = addressEditText.getText().toString();
		if (addressString.replaceAll("\\s+", "").length() == 0) {
			showWarningWithMsg("Please inpt your address.");
			return false;
		} // 2. check address line 1
		
		EditText cityEditText = (EditText)findViewById(R.id.txtCity);
		String cityString = cityEditText.getText().toString();
		if (cityString.replaceAll("\\s+", "").length() == 0)  {
			showWarningWithMsg("Please inpt your city.");
			return false;
		} // 3. check City
		
		EditText stateEditText = (EditText)findViewById(R.id.txtState);
		String stateString = stateEditText.getText().toString();
		if (stateString.replaceAll("\\s+", "").length() == 0) {
			showWarningWithMsg("Please inpt your state.");
			return false;
		} // 4. check state
		
		EditText zipEditText = (EditText)findViewById(R.id.txtZipcode);
		String zipString = zipEditText.getText().toString();
		if (zipString.replaceAll("\\s+", "").length() == 0) {
			showWarningWithMsg("Please inpt your zipcode.");
			return false;
		} // 5. check zipcode
		
		EditText telephoneEditText = (EditText)findViewById(R.id.txtTelephone);
		String telephoneString = telephoneEditText.getText().toString();
		if (telephoneString.replaceAll("\\s+", "").length() == 0) {
			showWarningWithMsg("Please inpt your telephone.");
			return false;
		} // 6. check telephone
		
		EditText emailEditText = (EditText)findViewById(R.id.txtEmail);
		String emailString = emailEditText.getText().toString();
		if (emailString.replaceAll("\\s+", "").length() == 0) {
			showWarningWithMsg("Please inpt your email address.");
			return false;
		} // 7. check email
		
		
		return true;
	}

	// save button clicked
	@Override
	public void onClick(View v) {
		// TODO: save address to server
		if (this.varifyInputContent()) {
			// TODO: save address to server
			finish();			
		}
		else {
			// test
			//finish();
		}
	}

}
